<?php
//============================================================+


require_once('./tcpdf/config/lang/ron.php');
require_once('./tcpdf/tcpdf.php');


define("DEFAULT_MAX_TEXT_LINE_LENGTH",     43);





// extend TCPF with custom functions
class MYPDF extends TCPDF {

	// Load table data from file
	public function LoadData($header) {
		// Read file lines
		$data = array();
		
		if ( isset($_POST['number_of_rows']) and  !empty($_POST['number_of_rows']) )
		{		
			for ($rowIndex = 0; $rowIndex < intval($_POST['number_of_rows']); $rowIndex++ )
			{
				
				$table_prefix = "tabel_".strval($rowIndex + 1 )."_";
				
				$data[$rowIndex][0] = strval($rowIndex + 1);
				
				if ( isset($_POST[$table_prefix.'denumire_material']) and  !empty($_POST[$table_prefix.'denumire_material']) )
					$data[$rowIndex][1] = $_POST[$table_prefix.'denumire_material'];
				else
					$data[$rowIndex][1] = "invalid";
					
				if ( isset($_POST[$table_prefix.'cantitate']) and  !empty($_POST[$table_prefix.'cantitate']) )
					if ( preg_match('/^\d+(\.\d+)?$/',$_POST[$table_prefix.'cantitate']) != 0 )	
						$data[$rowIndex][4] = $_POST[$table_prefix.'cantitate'];
					else
						$data[$rowIndex][4] = "invalid";
				else
					$data[$rowIndex][4] = "invalid";	
				
				if ( isset($_POST[$table_prefix.'unitate_masura']) and  !empty($_POST[$table_prefix.'unitate_masura']) )
					$data[$rowIndex][3] = $_POST[$table_prefix.'unitate_masura'];
				else
					$data[$rowIndex][3] = "invalid";
				
				if ( isset($_POST[$table_prefix.'data']) and  !empty($_POST[$table_prefix.'data']) )
					$data[$rowIndex][2] = $_POST[$table_prefix.'data'];
				else
					$data[$rowIndex][2] = "invalid";

				if ( isset($_POST[$table_prefix.'observatii']) and  !empty($_POST[$table_prefix.'observatii']) )	
					$data[$rowIndex][5] = $_POST[$table_prefix.'observatii'];
				else
					$data[$rowIndex][5] = "invalid";				
			}		
		}
		
		
		
		return $data;
	}

	// Colored table
	public function ColoredTable($header,$data) {
		// Colors, line width and bold font
		$this->SetFillColor(255, 255, 255);
		$this->SetTextColor(0);
		$this->SetDrawColor(0, 0, 0);
		$this->SetLineWidth(0.4);
		
		$this->setCellPaddings(0, 1, 0, 0);
		
		$this->SetFont('helvetica', '', 9);
		// Header
		$w= array(10,40,30,30,35,40);
		
		
		$num_headers = count($header);
		for($i = 0; $i < $num_headers; ++$i) {
			$this->MultiCell($w[$i], 10, $header[$i], 1, 'C', 1, 0, '', '', true, 0, false, true, 0 , 'M');
		}
		$this->Ln();
		// Color and font restoration
		$this->SetTextColor(0);
		$this->SetFont('helvetica', '', 9);
		// Data
		foreach($data as $row) {
			
			for ( $columnIndex = 0; $columnIndex < count($header); $columnIndex++ )
			{
					if( $row[$columnIndex] == "invalid" )
						$this->SetTextColor(255,0,0);			
					else
						$this->SetTextColor(0,0,0);
				
					$this->MultiCell($w[$columnIndex], 6, $row[$columnIndex], 1, 'C', 1, 0, '', '', true, 0, false, true, 0 , 'M');
			}
			
			$this->Ln();
		}
		
		$this->SetTextColor(0,0,0);
		
	}
	
	    //Page header
    public function Header() {
        // Logo
		$image_file = 'images'.DIRECTORY_SEPARATOR.'generate_pdf'.DIRECTORY_SEPARATOR.'antet1.jpg';
        $this->Image($image_file, '', '', '', '', 'JPG', '', 'T', false, 300, 'M', false, false, 'C', false, false, 'B');
		
    }

    // Page footer
    public function Footer() {
        // Position at 15 mm from bottom
        $this->SetY(-15);
        // Set font
        $this->SetFont('helvetica', 'I', 8);
        // Page number
        $this->Cell(0, 10, 'Pagina '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
    }
	
}

// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Bogdan');
$pdf->SetTitle('Table Example');
$pdf->SetSubject('Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set default header data
//$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 011', PDF_HEADER_STRING);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, 'Change Header', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, 'Change Footer', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//set some language-dependent strings
$pdf->setLanguageArray($l);

// ---------------------------------------------------------

// set font
$pdf->SetFont('helvetica', '', 12);

// add a page
$pdf->AddPage();

$pdf->SetY(50);
$pdf->SetFont('helvetica', 'BU', 14);
$pdf->MultiCell(0, 10, 'NECESAR MATERIALE:', 0, 'C', false, 0, '', '', true );

//$this->MultiCell($w[$i], 10, $header[$i], 1, 'C', 1, 0, '', '', true, 0, false, true, 0 , 'M');


$pdf->SetDrawColor(191, 191, 191);
$pdf->SetLineWidth(0.5);

$pdf->setCellPaddings(2, 3, 0, 0);

if ( isset($_POST['nume_proiect']) and  !empty($_POST['nume_proiect']) )
	$pdf->SetTextColor(0,0,0);
else
	$pdf->SetTextColor(255,0,0);

$pdf->SetY(65);
$pdf->SetFillColor(166, 166, 166);
$pdf->SetFont('helvetica', 'BI', 11);
$pdf->MultiCell(35, 10, 'PROIECT:', 'LB', 'L', true, 0, '', '', true );

$pdf->SetY(65);
$pdf->SetX(50);
$pdf->SetFont('helvetica', '', 11);
if ( isset($_POST['nume_proiect']) and  !empty($_POST['nume_proiect']) )
{		
	$pdf->MultiCell(70, 10, $_POST['nume_proiect'], 'B', 'C', false, 0, '', '', true );
}

if ( isset($_POST['data']) and  !empty($_POST['data']) )
	$pdf->SetTextColor(0,0,0);
else
	$pdf->SetTextColor(255,0,0);

$pdf->SetY(65);
$pdf->SetX(120);
$pdf->SetFont('helvetica', 'BI', 11);
$pdf->MultiCell(30, 10, 'DATA:', 'LB', 'L', true, 0, '', '', true );

$pdf->SetY(65);
$pdf->SetX(150);
$pdf->SetFont('helvetica', '', 11);
if ( isset($_POST['data']) and  !empty($_POST['data']) )
{		
	$pdf->MultiCell(50, 10, $_POST['nume_proiect'], 'B', 'C', false, 0, '', '', true);
}


if ( isset($_POST['de_la']) and  !empty($_POST['de_la']) )
	$pdf->SetTextColor(0,0,0);
else
	$pdf->SetTextColor(255,0,0);

$pdf->SetY(75);
$pdf->SetFillColor(166, 166, 166);
$pdf->SetFont('helvetica', 'BI', 11);
$pdf->MultiCell(35, 10, 'DE LA:', 'LB', 'L', true, 0, '', '', true );

$pdf->SetY(75);
$pdf->SetX(50);
$pdf->SetFont('helvetica', '', 11);
if ( isset($_POST['de_la']) and  !empty($_POST['de_la']) )
{		
	$pdf->MultiCell(150, 10, $_POST['de_la'], 'B', 'C', false, 0, '', '', true );
}

if ( isset($_POST['subiect']) and  !empty($_POST['subiect']) )
	$pdf->SetTextColor(0,0,0);
else
	$pdf->SetTextColor(255,0,0);

$pdf->SetY(85);
$pdf->SetFillColor(166, 166, 166);
$pdf->SetFont('helvetica', 'BI', 11);
$pdf->MultiCell(35, 10, 'SUBIECT:', 'LB', 'L', true, 0, '', '', true );

$pdf->SetY(85);
$pdf->SetX(50);
$pdf->SetFont('helvetica', '', 11);
if ( isset($_POST['subiect']) and  !empty($_POST['subiect']) )
{		
	$pdf->MultiCell(150, 10,  $_POST['subiect'], 'B', 'C', false, 0, '', '', true );
}


//Column titles
$header = array('NR CRT', 'DENUMIRE MATERIAL', 'CANTITATE', 'UNITATE MASURA', 'DATA ESTIMATA PENTRU LIVRARE', 'OBSERVATII');

//Data loading
$data = $pdf->LoadData($header);

$pdf->SetY(100);

// print colored table
$pdf->ColoredTable($header, $data);



$pdf->setCellPaddings(2, 4, 6, 8);
$pdf->SetFont('times', '', 10);
$pdf->MultiCell(40, 10, 'Completat,Sef Santier ing. ALEXANDRA CARACALEANU', 0, 'L', false, 0, '', '', true );
$pdf->SetX(150);
$pdf->SetFont('times', '', 10);
$pdf->MultiCell(40, 10, 'Verificat Projet  Manager', 0, 'L', false, 0, '', '', true );



// ---------------------------------------------------------

//Close and output PDF document
$pdf->Output('example_011.pdf', 'I');

//============================================================+
// END OF FILE                                                
//============================================================+
